<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

class Term extends Admin_Controller
{
	
	public $model = "term";
	public $view = "term";
	public $permissionCreate = 'Term.Create';
	public $permissionDelete = 'Term.Delete';
	public $permissionEdit = 'Term.Edit';
	public $permissionView = 'Term.View';
	public $term_allow = array('category', 'tag','link','event');
	
	protected $index_show = array(
								array('field' => 'term_id', 'label' => 'ID'),
								array('field' => 'term_name', 'label' => 'Name', 'custom_value' => '<a href="{url_edit}{term_id}">{term_name}</a>'),
								array('field' => 'term_description', 'label' => 'Description'),
							);

	function __construct() {
		parent::__construct();
		$this->load->model($this->model . '_m', $this->model);
		$this->load->model('Termmeta_m', 'termmeta');
		$this->_model = $this->{$this->model};
		$this->url_edit = $this->url_index . "/edit/";
        $this->url_delete = $this->url_index . "/delete/";
        $this->url_add = $this->url_index . "/edit/0";
	}
	
	function index($term_name = 'category', $menu = 0) {	
		$data = array();
		$this->permissionCreate = 'Term.'.ucfirst($term_name).'.Create';
		$this->permissionDelete = 'Term.'.ucfirst($term_name).'.Delete';
		$this->permissionEdit = 'Term.'.ucfirst($term_name).'.Edit';
		$this->permissionView = 'Term.'.ucfirst($term_name).'.View';
		//$this->termmeta->add_metadata(1,'323','xxx');
		if (!$term_name || !in_array($term_name, $this->term_allow)) {
			show_404();
		}
		$post = $this->input->post();
		
		$menu = (int)$menu;
		$data['menu_id'] = $menu;
		$data['menu_type'] = $term_name;
		$this->url_edit = $this->url_index . '/edit/' . $term_name . '/';
		$data['menu_tree'] = $this->_model->get_menu($menu, $term_name);
		$this->theme_data["main"] = $this->view . "/index";
		$this->template->viewAdmin($this->theme_data, $data);
	}

	function event()
	{
		$this->current_permission = 'Term.Event.View';
		$this->data['config']['ajax']['url'] = $this->url_index . '/event';

		$this->index_show[] = array('field' => 'term_order', 'label' => 'Danh sách tin','custom_value'=>'<a class="btn btn-default" href="{url_index}/view/event/{term_id}" data-fancybox-width="100%" data-fancybox-height="100%"><i class="fa fa-list-alt"></i> Danh sách tin</a>');
		 if($this->input->get('draw'))
		{
			$this->data_ajaxtable['request_data']['term_type'] = 'event';
			$this->url_edit = $this->url_edit.'event/';
			return parent::ajax_datatable();
		}
		
		$this->atheme->popup_add_posts();
		parent::index();
	}


	function view($taxanomy = 'category',$term_id = 0){
		$post_id = 0; $meta_key = '';
		$this->data['title'] = "Bài viết sự kiện";
		$this->current_permission = 'Term.'.$taxanomy.'.Edit';
		$this->check_permission();
		$this->data['post_id'] = $post_id;
		$this->data['meta_key'] = $meta_key;

		$this->load->model('post_m', 'post');
		// Lay danh sach post dang chay
		$posts = $this->post->get_posts(array('post_status' => 1,$taxanomy=> $term_id));
		$posts = array_map(function($obj){return $obj->post_id;}, $posts);
		$this->data['exits_ids'] = implode(',', $posts);

		$this->data['term'] = $this->term->get($term_id);
		$this->data['menu_tree'] = $this->term->get_menu(0, 'category');

		$this->breadcrumbs->push('Dòng sự kiện', '/term/event/',20);
		$this->breadcrumbs->push('Thêm tin sự kiện', '/term/view/event/'.$term_id,30);
		$this->theme_data["main"] = "term/event_posts";
		$this->template->viewAdmin($this->theme_data, $this->data);
	}

	function ajax_add_posts( $term_id = 0) {
		$this->load->model('term_posts_m', 'term_posts');
		$this->load->model('post_m');
		$post_ids = $this->input->post('post_id');
		if($post_ids){
			$this->term_posts->wp_set_term_posts($term_id,$post_ids,'event');
		}

		$result['data'] = $post_ids;
		$result['status'] = 1;
		die(json_encode($result));
	}

	function ajax_del_posts( $term_id = 0) {
		if($term_id == 0)
			return false;
		$this->load->model('term_posts_m', 'term_posts');
		$post_ids = (array)$this->input->post('post_id');
		$posts = $this->term_posts->wp_delete_term_posts($term_id,$post_ids);

		$result['data'] = $post_ids;
		$result['status'] = 1;
		die(json_encode($result));
	}

	function event_ajax_datatable_full(){
		$this->data_ajaxtable['request_data']['term_type'] = 'event';
        $this->url_edit = $this->url_edit.'event/';
        parent::ajax_datatable();
	}

	function link()
	{
		$data = array();
		if($link_id = $this->input->post('link_id'))
		{
			redirect($this->url_index.'/index/link/'.$link_id);
		}
		$links = $this->_model->where('term_parent',0)->get_many_by('term_type','link');
		$this->data['links'] = array();
		$this->data['links'][0] = 'Chọn link để sửa';
		if($links)
		{
			foreach($links as $link)
			{
				$this->data['links'][$link->term_id] = $link->term_name;
			}
		}
		
		$this->data['edit_id'] = 0;
		$this->theme_data["main"] = $this->view . "/link";
		$this->template->viewAdmin($this->theme_data, $this->data);
	}
	
	function add_url($parent = 0) {
		$name = $this->input->post("men_name");
		$status = $this->input->post("status");
		$parent = $this->input->post("men_parent");
		$slug = $this->input->post("slug");
		$type = $this->input->post("men_type");
		$id = (int)$this->input->post("men_id");
		if(!in_array($type, $this->term_allow))
			die("Type not allowed");
		$this->db->order_by('term_order desc');
		$men = $this->_model->get_by('term_parent', $parent);
		$status = (in_array($this->input->post('status'), array(0, 1))) ? $this->input->post('status') : 1;
		if (!$parent) $parent = 0;
		if (!$slug || $slug == '') $slug = $this->seo_m->seolink($name);
		$data_update = array('term_name' => $name, 'term_type' => $type, 'term_parent' => $parent, 'term_status' => $status, 'term_slug' => $slug,);
		if ($men) {
			$data_update['term_order'] = $men->term_order + 1;
		}
		$id = $this->_model->insert($data_update);

		$this->_model->build_link_cache($type);
		echo $id;
	}
	
	function save_order($menu_id = 0) {
		
		$json = $this->input->post("json");
		$object = json_decode($json, true);
		if (!$object) die;
		$type = '';
		if(isset($object[0]['type']))
		{
			$type = $object[0]['type'];
		}
		$this->_model->update_recursive($object, $menu_id);
		$this->_model->build_link_cache($type);
		echo $json;

		die;
	}
	
	function edit($type = 'category', $edit_id = 0) {
		if (!$type || !in_array($type, $this->term_allow)) {
			show_404();
		}
		$this->data['edit'] = $this->_model->where('term_type', $type)->get($edit_id);
		if ($edit_id >0 && $this->data['edit'] =='') {
			show_404();
		}
		$this->permissionCreate = 'Term.'.$type.'.Create';
		$this->permissionDelete = 'Term.'.$type.'.Delete';
		$this->permissionEdit = 'Term.'.$type.'.Edit';
		$this->permissionView = 'Term.'.$type.'.View';
		$this->url_edit = $this->url_index . '/edit/' . $type . '/';
		// $this->view_edit = $type;
		$this->view_edit = 'category';
		
		//rebuild url link cache
		$this->_model->build_link_cache($type);

		parent::edit($edit_id);
	}
	
	function del_child($menu_id = 0, $mitem = '') {
		$menu_id = (int)$menu_id;
		$type = '';
		$menu_tree = $this->_model->get_menu($menu_id);
		$this->_model->delete($menu_id);
		if ($menu_tree) foreach ($menu_tree as $menu) {
			$this->_model->delete($menu->term_id);
			if (isset($menu->child) && !empty($menu->child)) {
				foreach ($menu->child as $item) $this->del_child2($item->term_id, $item);
			}
			$type = $menu->term_type;
		}
		//rebuild url link cache
		$this->_model->build_link_cache($type);
	}
	
	private function del_child2($menu_id = 0, $mitem = '') {
		$this->_model->delete($menu_id);
		if ($mitem) if (isset($mitem->child) && !empty($mitem->child)) {
			foreach ($mitem->child as $item) $this->del_child2($item->term_id, $item);
		}
	}
}
